package models

import (
	"gitee.com/sansaniot/ssiot-core/httpmvc/model"

	"ssalarm/common/dto"
	"ssalarm/internal/alarm/models/query"
)

type SysDevVariable struct {
	Sn    string `json:"sn" gorm:"primaryKey;autoIncrement:false;type:varchar(64);comment:设备序列号"`
	Key   string `json:"key" gorm:"primaryKey;autoIncrement:false;type:varchar(255);comment:变量标识"`
	Label string `json:"label" gorm:"type:varchar(255);comment:变量名称"`
	Type  string `json:"type" gorm:"type:varchar(32);comment:变量类型"`
	Value string `json:"value" gorm:"type:text;comment:变量值"`

	model.Model
}

func (SysDevVariable) TableName() string {
	return "sys_dev_variable"
}

func (e *SysDevVariable) Generate() interface{} {
	o := *e
	return &o
}

func (e *SysDevVariable) GetNameMapper(sns []string) (mapper map[string]map[string]string) {
	mapper = make(map[string]map[string]string)
	list := make([]SysDevVariable, 0)
	cond := query.SysDevVariableQuery{}
	if len(sns) > 0 {
		cond.SnIn = sns
	}
	if err := e.FindList(&list, dto.MakeCondition(cond)); err != nil {
		return
	}
	for _, v := range list {
		if _, ok := mapper[v.Sn]; !ok {
			mapper[v.Sn] = make(map[string]string)
		}
		mapper[v.Sn][v.Key] = v.Label
	}
	return
}
